package com.android1.amarena2d.test;

import com.android1.amarena2d.actions.base.Action;
import com.android1.amarena2d.actions.base.IntervalAction;
import com.android1.amarena2d.actions.base.RepeatForever;
import com.android1.amarena2d.actions.base.Sequence;
import com.android1.amarena2d.actions.ease.EaseBounceIn;
import com.android1.amarena2d.actions.ease.EaseBounceOut;
import com.android1.amarena2d.actions.ease.EaseIn;
import com.android1.amarena2d.actions.ease.EaseOut;
import com.android1.amarena2d.actions.interval.DelayTime;
import com.android1.amarena2d.actions.movement.MoveBy;
import com.android1.amarena2d.nodes.FpsLabel;
import com.android1.amarena2d.nodes.Scene;
import com.android1.amarena2d.nodes.sprites.SharedSprite;
import com.android1.amarena2d.nodes.SimpleLabel;
import com.android1.amarena2d.texture.ManagedTexture;
import com.badlogic.gdx.graphics.Color;

public class SpriteEaseActionScene extends Scene {

    boolean running;

    SharedSprite sprite1;
    SharedSprite sprite2;
    SharedSprite sprite3;
    SharedSprite sprite4;
    SharedSprite sprite5;

    IntervalAction baseAction;

    MoveBy moveBy = MoveBy.$(420, 0, 3);

    ManagedTexture texture;

    public SpriteEaseActionScene() {

        touch().makeTouchable(this);

        setColor(Color.WHITE);
        FpsLabel fpsLabel = new FpsLabel(10, 20,Color.BLACK);
        add(fpsLabel);

        texture = engine.getTextureManager().getTexture("assets/testsprite.png");
        baseAction = Sequence.$(moveBy, moveBy.reverse(), DelayTime.$(2));

        sprite1 = new SharedSprite(texture, 20, 280);
        sprite1.action().run(wrap(baseAction.copy()));
        add(sprite1);

        sprite2 = new SharedSprite(texture, 20, 240);
        sprite2.action().run(wrap(EaseOut.$(2F, baseAction.copy())));
        add(sprite2);

        sprite3 = new SharedSprite(texture, 20, 200);
        sprite3.action().run(wrap(EaseIn.$(2F, baseAction.copy())));
        add(sprite3);

        sprite4 = new SharedSprite(texture, 20, 160);
        sprite4.action().run(wrap(EaseBounceIn.$(baseAction.copy())));
        add(sprite4);

        sprite5 = new SharedSprite(texture, 20, 120);
        sprite5.action().run(wrap(EaseBounceOut.$(baseAction.copy())));
        add(sprite5);

        add(new SimpleLabel("(Touch screen to start/stop)", 100, 20));


    }

    private Action wrap(IntervalAction action) {
        return RepeatForever.$(Sequence.$(action, DelayTime.$(2)));
    }

    @Override
    protected void onEnterFinished() {
        pauseAll();
    }

    @Override
    public boolean onTouchDown(float x, float y, int pointer, boolean isHit) {
        if (running)
            pauseAll();
        else
            resumeAll();
        running = !running;
        return true;
    }

    protected void resumeAll() {
        sprite1.action().resumeAll();
        sprite2.action().resumeAll();
        sprite3.action().resumeAll();
        sprite4.action().resumeAll();
        sprite5.action().resumeAll();
    }

    protected void pauseAll() {
        sprite1.action().pauseAll();
        sprite2.action().pauseAll();
        sprite3.action().pauseAll();
        sprite4.action().pauseAll();
        sprite5.action().pauseAll();
    }


}